Variation in resistance with changing light intensity
Applications
/* Name : main.c * Purpose : Source code for LDR Interfacing with ARM LPC1248. * Author : Gemicates * Date : 2018-09-02 * Website : www.gemicates.org * Revision : None */ #include<lpc214x.h> // header file for LPC21XX series #define LCD (0xff<<16) #define RS (1<<13) // register select pin #define RW (1<<14) // read write pin #define EN (1<<15) // enable pin #define LED (1<<31) void delay_fv(unsigned int x,int y); void lcd_display(unsigned int x); void cmd(unsigned char m); void lcd_ini(); void lcd_str(unsigned char *x); void adc_ini(); unsigned long int adc_data(); int main() { unsigned int val; PINSEL0=0X00000000; // select PORT0 as GPIO mode IO0DIR=0XFFFFFFFF; // make PORT0 pin as Output mode VPBDIV=0x02; adc_ini(); lcd_ini(); while(1) // Repeat(loop) forever { cmd(0x80); // clear screen lcd_str("LIGHT INT: "); val=adc_data(); lcd_display(val); if(val>800) { lcd_str("LOW "); IO0SET = LED; } else { lcd_str("HIGH"); IO0CLR = LED; } } } void delay_fv(unsigned int x,int y) // Time delay function in milli seconds { unsigned int i,j; for(i=0;i<x;i++) for(j=0;j<y;j++); } void lcd_display(unsigned int x) // Function to send data to LCD { IO0CLR|=(RS|RW|EN|LCD); IO0SET|=(x<<16); IO0SET|=RS; IO0CLR|=RW; IO0SET|=EN; delay_fv(100,200); IO0CLR|=EN; delay_fv(10,10); } void cmd(unsigned char m) // Function to send command to LCD { IO0CLR|=(RS|RW|EN|LCD); IO0SET|=(m<<16); IO0CLR|=RS; IO0CLR|=RW; IO0SET|=EN; delay_fv(100,10); IO0CLR|=EN; delay_fv(100,10); } void lcd_ini() // Funtion to Initialize LCD { cmd(0X38); // for using 8-bit 2 row mode and 5x7 Dots of LCD cmd(0X0C); // turn display ON for cursor OFF cmd(0X06); // display ON cmd(0X01); // clear screen cmd(0X80); // clear screen } void lcd_str(unsigned char *x) // Function to display in LCD { while(*x!='\0') { lcd_display(*x); x++; } } void adc_ini() { AD0CR = 1<<21; // A/D is Operational AD0CR = 0<<21; // A/D is in Power Down Mode PINSEL1 = 0x01000000; // P0.28 is Configured as Analog to Digital Converter Pin AD0.1 AD0CR = 0x00200802; // CLKDIV=4,Channel-0.1 Selected,BURST=0,EDGE=0 } unsigned long int adc_data() { unsigned long rec; AD0CR |= (1<<24); // Start Conversion while(!(AD0GDR & 0x80000000)); /*Wait untill the DONE bits Sets*/ rec = AD0GDR; AD0CR &= ~0x01000000; // Stops the A/D Conversion rec = rec >> 6; // data is present after 6 bit rec = rec & 0x3FF; // Clearing all other Bits return (rec); }